package com.timemaneger.thinkintime.custom;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.timemaneger.thinkintime.R;

import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * Created by JPZ on 2018/4/27.
 */

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.ViewHolder> {

    private List<Event> mEventList;
    public ViewHolder viewHolder;

    static class ViewHolder extends RecyclerView.ViewHolder{
        CheckBox mCheckBox;
        TextView mHeader;
        TextView mContent;
        TextView mDate;
        View eventView;

        public ViewHolder(View view){
            super(view);
            eventView = view;
            mCheckBox = (CheckBox) view.findViewById(R.id.event_item_checkBox);
            mHeader = (TextView) view.findViewById(R.id.event_item_header);
            mContent = (TextView) view.findViewById(R.id.event_item_contet);
            mDate = (TextView) view.findViewById(R.id.event_item_date);
        }
    }

    public EventAdapter(List<Event> eventList){
        mEventList=eventList;
    }

    public EventAdapter() {
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Event event=mEventList.get(position);
        holder.mHeader.setText(event.getHeader());
        holder.mContent.setText(event.getContent());
        holder.mDate.setText(event.getDate());
        holder.mCheckBox.setChecked(event.isFinished());
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_item,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        holder.eventView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(), "点击了事件项", Toast.LENGTH_SHORT).show();
            }
        });
        holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                  //  Toast.makeText(compoundButton.getContext(), "checkBox is checked", Toast.LENGTH_SHORT).show();
                    Event event=mEventList.get(holder.getAdapterPosition());
                    event.setFinished(true);
                    event.save();
                    if(CustomActivity.mSwitch.isChecked()){
                        removeItem(holder.getAdapterPosition());
                    }
                } else {
                   // Toast.makeText(compoundButton.getContext(), "checkBox is unChecked", Toast.LENGTH_SHORT).show();
                    Event event=mEventList.get(holder.getAdapterPosition());
                    event.setFinished(false);
                    event.save();
                }
            }
        });
        viewHolder = holder;
        return holder;
    }

    @Override
    public int getItemCount() {
        return mEventList.size();
    }


    //在指定位置插入，原位置的向后移动一格
    public boolean addItem(int position, Event msg) {
        if (position < mEventList.size() && position >= 0) {
            mEventList.add(position, msg);
            notifyItemInserted(position);
            notifyItemRangeChanged(position, mEventList.size() - position);
            return true;
        }
        return false;
    }

    public boolean addItem(Event event) {
        mEventList.add(event);
        notifyDataSetChanged();
        return true;
    }


    //去除指定位置的子项
    public boolean removeItem(int position) {
        if (position < mEventList.size() && position >= 0) {
            mEventList.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, mEventList.size() - position);
            return true;
        }
        return false;
    }

    //清空显示数据
    public void clearAll() {
        mEventList.clear();
        notifyDataSetChanged();
    }

    public List<Event> getmEventList() {
        return mEventList;
    }

    public void setmEventList(List<Event> mEventList) {
        this.mEventList.addAll(mEventList);
    }

    public Event getEventItem(int position) {
        if (position >= 0 && position < mEventList.size()) {
            return mEventList.get(position);
        }
        return null;
    }

}
